package com.toefl.speaking.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author lxw
 * 
 */
public class JsonUtil {
	private static final String TAG = "JsonUtil";

	public static String getSimpleJson(Map<String, Object> map) {
		JSONObject jsonObject = new JSONObject();

		Set<Map.Entry<String, Object>> entrySet = map.entrySet();

		for (Map.Entry<String, Object> entry : entrySet) {
			try {
				jsonObject.put(entry.getKey(), entry.getValue());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return jsonObject.toString();
	}

	/** 得到json字符串 */
	public static String getComplexJson(Map<String, Object> map) {
		JSONObject jsonObject = new JSONObject();
		Set<Map.Entry<String, Object>> entrySet = map.entrySet();
		for (Map.Entry<String, Object> entry : entrySet) {
			if (entry.getValue() instanceof Map) {
				getJson0(entry, jsonObject, true);
			} else {
				try {
					jsonObject.put(entry.getKey(), entry.getValue());
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		copy2File(jsonObject.toString());
		return jsonObject.toString();
	}

	private static void copy2File(String json) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File("/mnt/sdcard/json.txt"));
			fos.write(json.getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void getJson0(Map.Entry<String, Object> entry, JSONObject jsonObject, boolean isJsonArray) {
		if (!isJsonArray) {
			try {
				jsonObject.put(entry.getKey(), entry.getValue());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			JSONObject jsonObjectSub = new JSONObject();
			Object obj = entry.getValue();
			Map<String, Object> map = (Map<String, Object>) obj;
			for (Map.Entry<String, Object> en : map.entrySet()) {
				Object object = en.getValue();
				if (object instanceof Map) {
					getJson0(en, jsonObjectSub, true);
				} else {
					getJson0(en, jsonObjectSub, false);
				}
			}
			try {
				jsonObject.put(entry.getKey(), jsonObjectSub);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	// /**
	// *
	// * @param clazz1 要封装的bean
	// * @param respJson 服务端返回的json数据
	// * @return
	// */
	// public static Object parseJson2Object(Class clazz1, String respJson) {
	//
	// if (StringUtils.isEmpty(respJson)) {
	// return new ArrayList<Product>(0);
	// }
	// JSONObject jsonObject;
	// try {
	// jsonObject = new JSONObject(respJson);
	// if (clazz1 == Product.class) {
	// return parseJson2Products(jsonObject);
	// } else if (clazz1 == LoginPo.class) {
	// return paseJson2LoginPo(jsonObject);
	// } else if (clazz1 == Insurance.class) {
	// return parseJson2Insurance(jsonObject);
	// } else if (clazz1 == String.class) {
	// return parseJson2Career(jsonObject);
	// }
	// Log.i(TAG, respJson);
	// } catch (JSONException e) {
	// e.printStackTrace();
	// }
	// return null;
	// }

	private static Object parseJson2Career(JSONObject jsonObject) throws JSONException {
		JSONArray jsonArray = jsonObject.getJSONArray("respParam");
		List<String[]> careers = new ArrayList<String[]>(5);
		for (int i = 0; i < jsonArray.length(); i++) {
			careers.add(new String[] { jsonArray.getString(i), "" + 10110 + i });
		}
		return careers;
	}

	// private static Object parseJson2Insurance(JSONObject jsonObject) throws JSONException {
	//
	// JSONArray respRaramJson = jsonObject.getJSONArray("respParam");
	// List<Insurance> insurances = new ArrayList<Insurance>();
	// for (int i = 0; i < respRaramJson.length(); i++) {
	// JSONObject jsonObject2 = respRaramJson.getJSONObject(i);
	// Insurance insurance = new Insurance();
	// insurance.insuranceName = jsonObject2.getString("productName");
	// insurance.additionCategory = jsonObject2.getString("main");
	// insurances.add(insurance);
	// }
	//
	// return insurances;
	// }

	// private static Object paseJson2LoginPo(JSONObject jsonObject) throws JSONException {
	// LoginPo loginPo = new LoginPo();
	//
	// JSONObject respRaramJson = jsonObject.getJSONObject("respParam");
	// loginPo.birthday = respRaramJson.getString("birthday");
	// loginPo.sex = respRaramJson.getString("sex");
	// loginPo.team = respRaramJson.getString("ywtd");
	// loginPo.phone = respRaramJson.getString("tel");
	// loginPo.insuranceAgencyNo = respRaramJson.getString("zgzh");
	// loginPo.cerNumber = respRaramJson.getString("identityCardId");
	// loginPo.organization = respRaramJson.getString("ssjg");
	//
	// loginPo.empNo = respRaramJson.getString("yxgh");
	// loginPo.agentName = respRaramJson.getString("name");
	// loginPo.department = respRaramJson.getString("ssbm");
	// // 资格证号
	// loginPo.insuranceAgencyNo = respRaramJson.getString("zyzh");
	// loginPo.jobCode = respRaramJson.getString("zj");
	// loginPo.mobile = respRaramJson.getString("mobile");
	//
	// return loginPo;
	// }

	// private static List<Product> parseJson2Products(JSONObject jsonObject) throws JSONException {
	// List<Product> list = new ArrayList<Product>();
	// JSONObject respRaramJson = jsonObject.getJSONObject("respParam");
	// JSONArray plans = respRaramJson.getJSONArray("plans");
	// for (int i = 0; i < plans.length(); i++) {
	// JSONObject product_obj = plans.getJSONObject(i);
	// Product product = new Product();
	// product.productCode = product_obj.getString("productCode");
	// product.productName = product_obj.getString("productName");
	// product.isIndex = product_obj.getInt("mainPush") == 1 ? true : false;
	// product.productNote = product_obj.getString("introduction");
	// product.imageUrl = product_obj.getString("imgName");
	// list.add(product);
	// }
	// return list;
	// }
}
